package com.comment.controller;

import com.comment.annotation.JwtIgnore;
import com.comment.common.RestfulResult;
import com.comment.config.model.Audience;
import com.comment.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @className AdminUserController
 * @description: 
 * @author vedeng
 * @date 2021/12/21 11:09
*/
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminUserController {

    @Autowired
    private Audience audience;

    @PostMapping("/login")
    @JwtIgnore
    public RestfulResult adminLogin(HttpServletResponse response, String username, String password) {
        // 这里模拟测试, 默认登录成功，返回用户ID和角色信息
        String userId = "1";
        String role = "admin";

        // 创建token
        String token = JwtTokenUtil.createJWT(userId, username, role, audience);
        log.info("### 登录成功, token={} ###", token);

        // 将token放在响应头
        response.setHeader(JwtTokenUtil.AUTH_HEADER_KEY, JwtTokenUtil.TOKEN_PREFIX + token);
        // 将token响应给客户端
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        return RestfulResult.success(map);
    }

    @GetMapping("/users")
    public RestfulResult userList() {
        log.info("### 查询所有用户列表 ###");
        return RestfulResult.success();
    }
}
